﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudyCode.Generics.GenericsClass;

namespace StudyCode.Generics
{
    static class Program
    {
        static void Main()
        {
            //性能测试
            TestGenericsPerformance();

            //3重用，并且在多数不知道集合大小的时候，更加节约空间，
            //在大多数不知道集合大小的时候采用list而非数组
            {
                List<int> intList = new List<int>();

                int[] intArray = new int[1000];

                List<double> doubleList = new List<double>();

                List<long> longList = new List<long>();

                List<Student> studentList = new List<Student>();

            }

            var intDefault=DefauletEntity<int>();
            var studentDefault=DefauletEntity<Student>();

            var repository = new Repository<Student>();

            //4 可为空的值类型,加了？的意思是除了值类型可以区的值之外，还可以为null
            int? c = default(Nullable<int>);
            int? a = default(int?);
            int b = default(int);
           

         

            Console.WriteLine(a);
            Console.WriteLine(b);

            //Console.WriteLine(intDefault);
            //Console.WriteLine(studentDefault);

            Console.ReadLine();
        }

        /// <summary>
        /// 一般情况下的例子
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public  static  T DefauletEntity<T>()
        {
            //有操作有可能会失败
            //在操作失败的事情下，给出方法的返回值
            if (DateTime.Now.Second%2 == 0)
            {
                //成功，正常return
            }
            //5.失败，返回默认值
            return default(T);
        }
        
        /// <summary>
        /// 测试泛型和拆箱的性能
        /// 测试结果
        /// 装拆箱损耗是泛型损耗的10倍
        ///1. 泛型比装拆箱快10倍
        ///  </summary>
        static void TestGenericsPerformance()
        {
            //性能监视
            /* 性
             * 能
             * 监
             * 视*/
            var watch = Stopwatch.StartNew();
            var sum = 0;
            double sumDouble = 0.00;
            var arrayList = new ArrayList();
            for (var i = 0; i < 10000000; i++)
            {
                arrayList.Add(i);
            }
            foreach (object item in arrayList)
            {
                sum += (int)item;
                //2.如果错误的转换会导致类型转换错误，
                sumDouble += (double) item;
            }
            watch.Stop();
            Console.WriteLine("装拆箱花时间为;{0}毫秒", watch.ElapsedMilliseconds);
            sum = 0;
            watch.Restart();
            var list = new List<int>();
            for (var i = 0; i < 10000000; i++)
            {
                list.Add(i);
            }
            foreach (var item in list)
            {
                sum += item;
                //2.泛型的话，就会使用正常的转换
            }
            watch.Stop();
            Console.WriteLine("泛型花时间为;{0}毫秒", watch.ElapsedMilliseconds);


        }
    }
}
